const electron = require('electron')
// Module to control application life.
const app = electron.app
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
// const path=require("path")
app.allowRendererProcessReuse = false;
const { session } = require('electron');
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

//-----------------------------------------------------------------

const {Menu, MenuItem, dialog, ipcMain }=electron;
// const app_dir="pinshi"
const app_dir="."
//是否可以安全退出

let safeExit = false;

//-----------------------------------------------------------------

process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true

// Keep a global reference of the window object, if you don't, the window will

// be closed automatically when the JavaScript object is garbage collected.
// let windows=[];
let mainWindow;

ipcMain.on('getpath', (event, arg) => {
    event.returnValue = __dirname;
})
ipcMain.on('setcookies', (event, arg) => {
    setcookies(arg);
})

// ipcMain.handle("showOpenDialog", (event, options) => {
//   return dialog.showOpenDialogSync(options);
// })
// ipcMain.handle("showSaveDialog", (event, options) => {
//   return dialog.showSaveDialogSync(options);
// })
ipcMain.handle('showOpenDialog', async function (e, options) {
    const result = await dialog.showOpenDialog(mainWindow, options)
    return result.filePaths
})

ipcMain.handle('showSaveDialog', async function (e, options) {
  const result = await dialog.showSaveDialog(mainWindow, options)
  return result.filePath
})
function setcookies(arg){
  for(var i=0;i<arg.length;i++){
    let cookie=arg[i];
    session.defaultSession.cookies.set(cookie, (error) => {
      if (error) console.error(error);
    })
  }
  console.log("ok");
}

ipcMain.on('setcookie', (event, arg) => {
    setcookie(arg);
})

function setcookie(arg){
  const cookie = { url: arg.url, name: arg.name, value: arg.value};
  session.defaultSession.cookies.set(cookie, (error) => {
    if (error) console.error(error);
  })
  console.log("ok");
}
const createWindow = (url) => {
  console.log("createWindow");

  // Create the browser window.

  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences:{
      // preload: path.join(__dirname, 'preload.js'),
      contextIsolation: false,
      nodeIntegration:true,
      enableRemoteModule: true
    }

  });
  // console.log(windows);
  // windows.push(mainWindow)
  //menu
  const template=
    [{
      label: 'File',
      submenu: [
        {
          label: 'New Window',
          accelerator: 'Ctrl+N',
          click: () =>{createWindow(null)},
        },
        {
          label: 'HOME',
          accelerator: 'Ctrl+H',
          click: (item, win) =>{win.loadURL(`file://${__dirname}/${app_dir}/index.html`);},
        },
         {
          label: 'Forward',
          accelerator: 'Ctrl+F',
          click: (item, win) =>{
            win.webContents.goForward();
          },
        },
        {
          label: 'BACK',
          accelerator: 'Ctrl+B',
          click: (item, win) =>{
            win.webContents.goBack();
          },
        },
        {
          label: 'RELOAD',
          accelerator: 'Ctrl+R',
          click: (item, win) =>{
            win.webContents.reload();
          },
        },
        {
          label: 'SAVE',
          accelerator: 'Ctrl+S',
          click: (item, win) =>{
            for(i in win.webContents){
              console.log(i);
            }
            win.webContents.send("save");
          },
        },
        {
          label: 'Print',
          accelerator: 'Ctrl+P',
          click: (item, win) =>{
            // console.log("printToPDF")
            win.webContents.executeJavaScript("window.print();");
          },
        },
        {
          label: 'DevTools',
          accelerator: 'Ctrl+D',
          click: (item, win) =>{
            mainWindow.openDevTools();
          },
        },
        {
          label: 'Exit',
          accelerator: 'Ctrl+E',
          click: (item, win) =>{
             // console.log(win);
             // console.log(mainWindow);
             win.close();
          },
        }
        ]
    }];
  const menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);

  mainWindow.openDevTools();
  if(url)
    mainWindow.loadURL(url);  
  else{
    mainWindow.loadURL(`file://${__dirname}/${app_dir}/index.html`);
  }



  // Open the DevTools.

  // /mainWindow.webContents.openDevTools();
  // mainWindow.on('close', (e) => {

  //   if(!safeExit){

  //     e.preventDefault();

  //     mainWindow.webContents.send('action', 'exiting');

  //   }

  // });

  //-----------------------------------------------------------------
  mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options, additionalFeatures) => {
      console.log(event);
      // console.log("url=================")
      console.log(url);
      console.log(event.url)
      // console.log("frameName===========")
      // console.log(frameName)
      // console.log("disposition==============")
      // console.log(disposition)
      // console.log("options===========")
      // console.log(options)
      // console.log(options.webContents.getURL())
      // console.log("additionalFeatures===========")
      // console.log(additionalFeatures)
      // if (disposition==="foreground-tab"){
      //   return
      // }
      // else{
        event.preventDefault()
        createWindow(url);
      // }
    // if (frameName === 'modal') {
    //   // open window as modal
    //   event.preventDefault()
    //   Object.assign(options, {
    //     modal: false,
    //     parent: null,
    //     width: 100,
    //     height: 100
    //   })
    //   event.newGuest = new BrowserWindow(options)
    // }
  })


  // Emitted when the window is closed.

  mainWindow.on('closed', () => {

    // Dereference the window object, usually you would store windows

    // in an array if your app supports multi windows, this is the time

    // when you should delete the corresponding element.
    // for(let i=0;i<windows.length;i++){
    //   if (windows[i]===mainWindow)
    //   {
    //     delete windows[i];
    //   }
    // }
    mainWindow = null;
  });

};



// This method will be called when Electron has finished

// initialization and is ready to create browser windows.

// Some APIs can only be used after this event occurs.

app.on('ready', ()=>{
  createWindow(null)
});



// Quit when all windows are closed.

app.on('window-all-closed', () => {
  console.log("window-all-closed");
  // On OS X it is common for applications and their menu bar

  // to stay active until the user quits explicitly with Cmd + Q

  if (process.platform !== 'darwin') {

    app.quit();

  }

});



app.on('activate', () => {

  // On OS X it's common to re-create a window in the app when the

  // dock icon is clicked and there are no other windows open.

  if (mainWindow === null) {

    createWindow(null);

  }

});


